import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import '../components/PageWrap.dart';

class DioLn extends StatefulWidget {
  const DioLn({super.key});

  @override
  State<DioLn> createState() => _DioLnState();
}

class _DioLnState extends State<DioLn> {
  Dio _dio = Dio();

  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            "http库  dio", // 设置标题文本
            style: TextStyle(
              color: Colors.white, // 设置标题字体颜色
            ),
          ),
          iconTheme: IconThemeData(
            color: Colors.white, // 设置返回图标颜色
          ),
          backgroundColor: Colors.deepPurple, // 设置AppBar背景颜色
        ),
        body: Container(
          alignment: Alignment.center,
          child: FutureBuilder(
              future:
                  _dio.get("https://api.github.com/orgs/flutterchina/repos"),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                //请求完成
                if (snapshot.connectionState == ConnectionState.done) {
                  Response response = snapshot.data;
                  //发生错误
                  if (snapshot.hasError) {
                    return Text(snapshot.error.toString());
                  }
                  //请求成功，通过项目信息构建用于显示项目名称的ListView
                  return ListView(
                    children: response.data
                        .map<Widget>(
                            (e) => ListTile(title: Text(e["full_name"])))
                        .toList(),
                  );
                }
                //请求未完成时弹出loading
                return CircularProgressIndicator();
              }),
        ));
  }
}
